package pony.myfilters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/*  這個 Filter 完全是我自己的實作
 *  主要而且唯一的任務就是攔截所有來自Client Side的請求，
 *  先把請求物件的 Character Encoding 設定成UTF-8
 */

public class MySetCharacterEncodingFilter implements Filter {
	FilterConfig filterConfig;
	
	@Override
	public void destroy() {
		// 不做任何事!
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
			throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");  // 攔截請求，並先設定request的字元編碼
//		response.setContentType("text/html; charset=UTF-8");  
		// 攔截請求，並先設定response的MIME typr與字元編碼
		
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}
	
}
